class Course:
    def __init__(self, name, teacher, time, location):
        self.name = name
        self.teacher = teacher
        self.time = time
        self.location = location

    def __str__(self):
        return f"课程名: {self.name}, 教师: {self.teacher}, 时间: {self.time}, 地点: {self.location}"

class Student:
    def __init__(self, student_id, name, contact):
        self.student_id = student_id
        self.name = name
        self.contact = contact
        self.registered_courses = []

    def register_course(self, course):
        self.registered_courses.append(course)

    def __str__(self):
        return f"学生ID: {self.student_id}, 姓名: {self.name}, 联系方式: {self.contact}"

class Teacher:
    def __init__(self, teacher_id, name):
        self.teacher_id = teacher_id
        self.name = name

    def view_class_lists(self, database):
        class_lists = database.generate_class_lists()
        print(f"教师 {self.name} 查看班级列表：")
        for course_name, students in class_lists.items():
            print(f"{course_name}:")
            for student in students:
                print(f"  - {student.name}")

class Database:
    def __init__(self):
        self.courses = []
        self.students = []
        self.registrations = []

    def add_course(self, course):
        self.courses.append(course)
        print(f"成功添加课程: {course.name}")

    def add_student(self, student):
        self.students.append(student)
        print(f"成功添加学生: {student.name}")

    def student_register(self, student_id, course_name):
        student = self.get_student(student_id)
        course = self.get_course(course_name)
        if student and course:
            student.register_course(course)
            self.registrations.append((student_id, course_name))
            print(f"{student.name} 成功注册课程: {course.name}")
            return True
        print(f"注册失败，学生或课程不存在。")
        return False

    def get_student(self, student_id):
        for student in self.students:
            if student.student_id == student_id:
                return student
        return None

    def get_course(self, course_name):
        for course in self.courses:
            if course.name == course_name:
                return course
        return None

    def generate_class_lists(self):
        class_lists = {}
        for registration in self.registrations:
            student_id, course_name = registration
            student = self.get_student(student_id)
            if course_name in class_lists:
                class_lists[course_name].append(student)
            else:
                class_lists[course_name] = [student]
        return class_lists

import unittest

class TestCourseRegistration(unittest.TestCase):
    def setUp(self):
        self.db = Database()
        # 添加课程
        self.db.add_course(Course("数学", "张老师", "周一3 - 4节", "A101"))
        self.db.add_course(Course("英语", "李老师", "周二1 - 2节", "B202"))
        # 添加学生
        self.db.add_student(Student("S001", "张三", "123456789"))
        self.db.add_student(Student("S002", "李四", "987654321"))
        # 添加教师
        self.teacher = Teacher("T001", "王老师")

    def test_student_register(self):
        # 测试学生注册课程
        result1 = self.db.student_register("S001", "数学")
        result2 = self.db.student_register("S002", "数学")
        self.assertTrue(result1)
        self.assertTrue(result2)
        # 测试注册不存在的课程
        result3 = self.db.student_register("S001", "不存在的课程")
        self.assertFalse(result3)

    def test_generate_class_lists(self):
        # 先注册一些学生
        self.db.student_register("S001", "数学")
        self.db.student_register("S002", "数学")
        class_lists = self.db.generate_class_lists()
        print("班级列表:")
        for course_name, students in class_lists.items():
            print(f"{course_name}:")
            for student in students:
                print(f"  - {student.name}")
        self.assertEqual(len(class_lists["数学"]), 2)
        self.assertEqual(class_lists["数学"][0].name, "张三")
        self.assertEqual(class_lists["数学"][1].name, "李四")
        # 确保未注册的课程没有班级列表
        self.assertNotIn('英语', class_lists)

    def test_teacher_view_class_lists(self):
        # 注册一些学生
        self.db.student_register("S001", "数学")
        self.db.student_register("S002", "数学")
        # 教师查看班级列表
        self.teacher.view_class_lists(self.db)

if __name__ == '__main__':
    unittest.main()